// pages/orders/orders.js
Page({
  data: {
    orders: [],
    currentStatus: 'all', // 当前选中的订单状态
    statusList: [
      { id: 'all', name: '全部' },
      { id: 'toPay', name: '待付款' },
      { id: 'toShip', name: '待发货' },
      { id: 'toReceive', name: '待收货' },
      { id: 'toReview', name: '待评价' }
    ],
    statusTextMap: {
      'toPay': '待付款',
      'toShip': '待发货',
      'toReceive': '待收货',
      'toReview': '待评价',
      'completed': '已完成',
      'cancelled': '已取消'
    },
    contentVisible: false // 默认设置为不可见
  },

  
  checkContentVisibility: function() {
    const app = getApp();
    const isVisible = app.checkContentVisibility();
    this.setData({
      contentVisible: isVisible
    });
    return isVisible;
  },

  onLoad: function (options) {
    this.checkContentVisibility();
    
    if (!this.data.contentVisible) {
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        });
      }, 100);
      return;
    }

    if (options.status) {
      this.setData({
        currentStatus: options.status
      });
    }
    this.loadOrders();
  },

  onShow: function () {
    this.loadOrders();
  },

  // 加载订单数据
  loadOrders: function () {
    if (!this.checkContentVisibility()) {
      return;
    }
    
    const orders = wx.getStorageSync('orders') || [];
    const processedOrders = orders.map(order => ({
      ...order,
      statusText: this.data.statusTextMap[order.status] || order.status
    }));
    
    let filteredOrders = processedOrders;
    if (this.data.currentStatus !== 'all') {
      filteredOrders = processedOrders.filter(order => order.status === this.data.currentStatus);
    }

    this.setData({
      orders: filteredOrders
    });
  },

  // 切换订单状态
  switchStatus: function (e) {
    const status = e.currentTarget.dataset.status;
    this.setData({
      currentStatus: status
    });
    this.loadOrders();
  },

  // 取消订单
  cancelOrder: function (e) {
    const orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要取消该订单吗？',
      success: (res) => {
        if (res.confirm) {
          let orders = wx.getStorageSync('orders') || [];
          orders = orders.map(order => {
            if (order.id === orderId) {
              order.status = 'cancelled';
              order.statusText = this.data.statusTextMap['cancelled'];
            }
            return order;
          });
          wx.setStorageSync('orders', orders);
          this.loadOrders();
          wx.showToast({
            title: '订单已取消',
            icon: 'success'
          });
        }
      }
    });
  },

  // 确认收货
  confirmReceive: function (e) {
    const orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认已收到商品？',
      success: (res) => {
        if (res.confirm) {
          let orders = wx.getStorageSync('orders') || [];
          orders = orders.map(order => {
            if (order.id === orderId) {
              order.status = 'toReview';
              order.statusText = this.data.statusTextMap['toReview'];
            }
            return order;
          });
          wx.setStorageSync('orders', orders);
          this.loadOrders();
          wx.showToast({
            title: '已确认收货',
            icon: 'success'
          });
        }
      }
    });
  },

  // 去支付
  goPay: function (e) {
    const orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认支付该订单？',
      success: (res) => {
        if (res.confirm) {
          let orders = wx.getStorageSync('orders') || [];
          orders = orders.map(order => {
            if (order.id === orderId) {
              order.status = 'toShip';
              order.statusText = this.data.statusTextMap['toShip'];
            }
            return order;
          });
          wx.setStorageSync('orders', orders);
          this.loadOrders();
          wx.showToast({
            title: '支付成功',
            icon: 'success'
          });
        }
      }
    });
  },

  // 去评价
  goReview: function (e) {
    const orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认评价该订单？',
      success: (res) => {
        if (res.confirm) {
          let orders = wx.getStorageSync('orders') || [];
          orders = orders.map(order => {
            if (order.id === orderId) {
              order.status = 'completed';
              order.statusText = this.data.statusTextMap['completed'];
            }
            return order;
          });
          wx.setStorageSync('orders', orders);
          this.loadOrders();
          wx.showToast({
            title: '评价成功',
            icon: 'success'
          });
        }
      }
    });
  }
}); 